package cn.newtouch.order.service.impl;

import cn.newtouch.basic.service.impl.BaseServiceImpl;
import cn.newtouch.basic.util.PageList;
import cn.newtouch.order.domain.OrderPetAcquisition;
import cn.newtouch.order.dto.orderDto;
import cn.newtouch.order.mapper.OrderPetAcquisitionMapper;
import cn.newtouch.order.query.OrderPetAcquisitionQuery;
import cn.newtouch.order.service.IOrderPetAcquisitionService;
import cn.newtouch.org.domain.Employee;
import cn.newtouch.org.mapper.EmployeeMapper;
import cn.newtouch.org.mapper.ShopMapper;
import cn.newtouch.pet.domain.Pet;
import cn.newtouch.pet.domain.SearchMasterMsg;
import cn.newtouch.pet.mapper.PetDetailMapper;
import cn.newtouch.pet.mapper.PetMapper;
import cn.newtouch.pet.mapper.SearchMasterMsgMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * 业务实现类：
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {

    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private PetDetailMapper petDetailMapper;

    @Override
    public PageList<OrderPetAcquisition> queryPage(OrderPetAcquisitionQuery query, Long id) {

        Employee employee = employeeMapper.loadByLogininfoId(id);
        // 判断登录员工是哪种角色
        Long shop_id = employee.getShop_id();
        if(shop_id!=null){
            //店铺管理人员
            if(shopMapper.loadById(shop_id).getAdmin_id()!=employee.getId()){
                //店员
                query.setEmpId(employee.getId());
            }
            query.setShopId(shop_id);
        }
        return super.queryPage(query);
    }

    /**
     * 确认订单
     * @param dto
     * @param id
     */
    @Override
    public void confirm(orderDto dto, Long id) {
        Long orderId = dto.getId();
        BigDecimal money = dto.getMoney();
        Integer payType = dto.getPayType();
        //1. 改订单状态，金额, 支付方式
        OrderPetAcquisition opa = orderPetAcquisitionMapper.loadById(orderId);
        opa.setState(2);
        opa.setPrice(money);
        opa.setPaytype(payType);
        orderPetAcquisitionMapper.update(opa);

        //2. 改寻主消息状态
        SearchMasterMsg smm = searchMasterMsgMapper.loadById(opa.getSearch_master_msg_id());
        smm.setState(2);
        searchMasterMsgMapper.update(smm);

        //3. 改pet金额
        Pet pet = petMapper.loadById(opa.getPet_id());
        pet.setCostprice(money);
        petMapper.update(pet);
    }

    /**
     * 取消订单
     * @param id
     */
    @Override
    public void cancel(Long id) {
        //1.改变searchMasterMsg中的shopid=null
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.loadById(id);
        Long smmid = orderPetAcquisition.getSearch_master_msg_id();
        searchMasterMsgMapper.reject(smmid);

        //2.改变订单的状态
        orderPetAcquisition.setState(-1);
        orderPetAcquisitionMapper.update(orderPetAcquisition);

        //3.删除pet和相关详情
        Long petId = orderPetAcquisition.getPet_id();
        petMapper.remove(petId);

        petDetailMapper.removeByPetId(petId);


    }
}
